#include "..\StdAfx.h"
#include "UIDeviceMainFrameWnd.h"


//class CContectCtrl : public CContainerUI, public INotifyUI, public IDialogBuilderCallback

CUIDeviceMainFrameWnd::CUIDeviceMainFrameWnd(void)
{

}


CUIDeviceMainFrameWnd::~CUIDeviceMainFrameWnd(void)
{
}


void CUIDeviceMainFrameWnd::OnFinalMessage(HWND /*hWnd*/) 
{ 
	m_pm.RemovePreMessageFilter(this);
	delete this; 
}
void CUIDeviceMainFrameWnd::Init() 
{
}

void CUIDeviceMainFrameWnd::Notify(TNotifyUI& msg)
{
	if(msg.sType == _T("click") )
	{
		if(msg.pSender->GetName() == _T("ImportContacts"))
		{

		}
		else if (msg.pSender->GetName() == _T("newphone"))
		{
			ShowNewPhoneTab();
		}
		else if (msg.pSender->GetName() == _T("download"))
		{
			ShowDownLoadTab();
		}
		else if (msg.pSender->GetName() == _T("phonecopy"))
		{
			ShowTable(2);
		}
		else if (msg.pSender->GetName() == _T("importcontacts"))
		{
			ShowTable(3);
		}
		else if (msg.pSender->GetName() == _T("favorites"))
		{
			ShowFavoritesTab();
		}
	}
	else if(msg.sType == _T("itemselect"))
	{

	}
	else if (msg.sType == _T("selectchanged"))
	{

	}
}
void CUIDeviceMainFrameWnd::ShowTable(int iIndex)
{
	if (iIndex < 0 || iIndex > 10)
	{
		return;
	}
	CTabLayoutUI* pControl = static_cast<CTabLayoutUI*>(m_pm.FindControl(_T("switch")));
	pControl->SelectItem(iIndex);

}
LRESULT CUIDeviceMainFrameWnd::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	LONG lStyleValue = ::GetWindowLong(*this, GWL_STYLE);
	lStyleValue &= ~WS_CAPTION;
	::SetWindowLong(*this, GWL_STYLE, lStyleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_CHILDWINDOW);

// 	m_pm.Init(m_hWnd);
// 	m_pm.AddPreMessageFilter(this);
// 	CDialogBuilder builder;
// 	CControlUI* pRoot = builder.Create(_T("contect.xml"), (UINT)0, NULL, &m_pm);
// 	ASSERT(pRoot && "Failed to parse XML");
// 	m_pm.AttachDialog(pRoot);
// 	m_pm.AddNotifier(this);
// 
// 	Init();
	return 0;
}

LRESULT CUIDeviceMainFrameWnd::OnNcActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	if( ::IsIconic(*this) ) bHandled = FALSE;
	return (wParam == 0) ? TRUE : FALSE;
}




LRESULT CUIDeviceMainFrameWnd::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	SIZE szRoundCorner = m_pm.GetRoundCorner();
	if( !::IsIconic(*this) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0) ) {
		CRect rcWnd;
		::GetWindowRect(*this, &rcWnd);
		rcWnd.Offset(-rcWnd.left, -rcWnd.top);
		rcWnd.right++; rcWnd.bottom++;
		HRGN hRgn = ::CreateRoundRectRgn(rcWnd.left, rcWnd.top, rcWnd.right, rcWnd.bottom, szRoundCorner.cx, szRoundCorner.cy);
		::SetWindowRgn(*this, hRgn, TRUE);
		::DeleteObject(hRgn);
	}

	bHandled = FALSE;
	return 0;
}

LRESULT CUIDeviceMainFrameWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	LRESULT lRes = 0;
	BOOL bHandled = TRUE;
	switch( uMsg ) {
	case WM_CREATE:        lRes = OnCreate(uMsg, wParam, lParam, bHandled); break;
	case WM_SIZE:          lRes = OnSize(uMsg, wParam, lParam, bHandled); break;
	default:
		bHandled = FALSE;
	}
	if( bHandled ) return lRes;
	if( m_pm.MessageHandler(uMsg, wParam, lParam, lRes) ) return lRes;
	return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
}

LRESULT CUIDeviceMainFrameWnd::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, bool& bHandled)
{
	if( uMsg == WM_KEYDOWN ) 
	{
		if( wParam == VK_RETURN ) 
		{
			return true;
		}
		else if( wParam == VK_ESCAPE ) 
		{
			return true;
		}
	}
	return false;
}

void CUIDeviceMainFrameWnd::ShowNewPhoneTab()
{
	

	ShowTable(0);
}
void CUIDeviceMainFrameWnd::ShowDownLoadTab()
{

// 	pcr.appdear.com/start/download.html?phonetype=.
// 	pcr.appdear.com/start/download.html?phonetype=3.
// 	pcr.appdear.com/start/download.html?phonetype=4.41
// 	pcr.appdear.com/start/download.html?phonetype=4.42


	ShowTable(1);
}

void CUIDeviceMainFrameWnd::ShowFavoritesTab()
{

	ShowTable(4);
}